/**
 * 优惠券服务
 * 管理所有优惠券相关的API调用
 */

const BASE_URL = 'http://localhost:8081';

class CouponService {
	/**
	 * 获取优惠券列表
	 * @param {string} status - 优惠券状态: 'unused' | 'used' | 'expired' | 'all'
	 * @returns {Promise} 优惠券列表
	 */
	async getCouponList(status = 'all') {
		try {
			const token = uni.getStorageSync('token');
			if (!token) {
				throw new Error('用户未登录');
			}

			const response = await uni.request({
				url: `${BASE_URL}/coupons/list`,
				method: 'GET',
				data: { status },
				header: { 
					'Authorization': token,
					'Content-Type': 'application/json'
				}
			});

			if (response.statusCode === 200 && response.data.success) {
				return response.data.data;
			} else {
				throw new Error(response.data.message || '获取优惠券失败');
			}
		} catch (error) {
			console.error('获取优惠券列表失败:', error);
			throw error;
		}
	}

	/**
	 * 兑换优惠券
	 * @param {string} code - 兑换码
	 * @returns {Promise} 兑换结果
	 */
	async exchangeCoupon(code) {
		try {
			const token = uni.getStorageSync('token');
			if (!token) {
				throw new Error('用户未登录');
			}

			if (!code || !code.trim()) {
				throw new Error('兑换码不能为空');
			}

			const response = await uni.request({
				url: `${BASE_URL}/coupons/exchange`,
				method: 'POST',
				data: { code: code.trim() },
				header: { 
					'Authorization': token,
					'Content-Type': 'application/json'
				}
			});

			if (response.statusCode === 200 && response.data.success) {
				return response.data.data;
			} else {
				throw new Error(response.data.message || '兑换失败');
			}
		} catch (error) {
			console.error('兑换优惠券失败:', error);
			throw error;
		}
	}

	/**
	 * 使用优惠券
	 * @param {string} couponId - 优惠券ID
	 * @param {string} orderId - 订单ID
	 * @returns {Promise} 使用结果
	 */
	async useCoupon(couponId, orderId) {
		try {
			const token = uni.getStorageSync('token');
			if (!token) {
				throw new Error('用户未登录');
			}

			if (!couponId) {
				throw new Error('优惠券ID不能为空');
			}

			const response = await uni.request({
				url: `${BASE_URL}/coupons/use`,
				method: 'POST',
				data: { 
					couponId,
					orderId: orderId || null
				},
				header: { 
					'Authorization': token,
					'Content-Type': 'application/json'
				}
			});

			if (response.statusCode === 200 && response.data.success) {
				return response.data.data;
			} else {
				throw new Error(response.data.message || '使用优惠券失败');
			}
		} catch (error) {
			console.error('使用优惠券失败:', error);
			throw error;
		}
	}

	/**
	 * 获取优惠券详情
	 * @param {string} couponId - 优惠券ID
	 * @returns {Promise} 优惠券详情
	 */
	async getCouponDetail(couponId) {
		try {
			const token = uni.getStorageSync('token');
			if (!token) {
				throw new Error('用户未登录');
			}

			if (!couponId) {
				throw new Error('优惠券ID不能为空');
			}

			const response = await uni.request({
				url: `${BASE_URL}/coupons/detail/${couponId}`,
				method: 'GET',
				header: { 
					'Authorization': token,
					'Content-Type': 'application/json'
				}
			});

			if (response.statusCode === 200 && response.data.success) {
				return response.data.data;
			} else {
				throw new Error(response.data.message || '获取优惠券详情失败');
			}
		} catch (error) {
			console.error('获取优惠券详情失败:', error);
			throw error;
		}
	}

	/**
	 * 获取可用优惠券（用于下单时选择）
	 * @param {number} amount - 订单金额
	 * @param {string} category - 商品分类
	 * @returns {Promise} 可用优惠券列表
	 */
	async getAvailableCoupons(amount, category = null) {
		try {
			const token = uni.getStorageSync('token');
			if (!token) {
				throw new Error('用户未登录');
			}

			if (!amount || amount <= 0) {
				throw new Error('订单金额无效');
			}

			const response = await uni.request({
				url: `${BASE_URL}/coupons/available`,
				method: 'GET',
				data: { 
					amount,
					category
				},
				header: { 
					'Authorization': token,
					'Content-Type': 'application/json'
				}
			});

			if (response.statusCode === 200 && response.data.success) {
				return response.data.data;
			} else {
				throw new Error(response.data.message || '获取可用优惠券失败');
			}
		} catch (error) {
			console.error('获取可用优惠券失败:', error);
			throw error;
		}
	}

	/**
	 * 分享优惠券
	 * @param {string} couponId - 优惠券ID
	 * @returns {Promise} 分享结果
	 */
	async shareCoupon(couponId) {
		try {
			const token = uni.getStorageSync('token');
			if (!token) {
				throw new Error('用户未登录');
			}

			if (!couponId) {
				throw new Error('优惠券ID不能为空');
			}

			const response = await uni.request({
				url: `${BASE_URL}/coupons/share`,
				method: 'POST',
				data: { couponId },
				header: { 
					'Authorization': token,
					'Content-Type': 'application/json'
				}
			});

			if (response.statusCode === 200 && response.data.success) {
				return response.data.data;
			} else {
				throw new Error(response.data.message || '分享优惠券失败');
			}
		} catch (error) {
			console.error('分享优惠券失败:', error);
			throw error;
		}
	}
}

// 导出单例
export default new CouponService(); 